﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace SQLH2ConfigTool
{
    public partial class frmAddCounter : Form
    {
        private Counter _SelectedCounter;
        public Counter SelectedCounter
        {
            get { return _SelectedCounter; }
            set { _SelectedCounter = value; }
        }

        Server _s;
        public frmAddCounter(Server s)
        {
            _s = s;
            InitializeComponent();

            try
            {
                //get performance counter categorys
                PerformanceCounterCategory[] perfCounters = PerformanceCounterCategory.GetCategories(s.ServerName);
                for (int i = 0; i < perfCounters.Length; i++)
                {
                    lstCategory.Items.Add(perfCounters[i].CategoryName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void lstCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            string[] instanceNames;
            System.Collections.ArrayList counters = new System.Collections.ArrayList();

            if (lstCategory.SelectedIndex != -1)
            {
                System.Diagnostics.PerformanceCounterCategory mycat = new System.Diagnostics.PerformanceCounterCategory(this.lstCategory.SelectedItem.ToString());
                // Remove the current contents of the list.
                this.lstCounter.Items.Clear();

                // Retrieve the counters.
                instanceNames = mycat.GetInstanceNames();

                if (instanceNames.Length == 0)
                {
                    counters.AddRange(mycat.GetCounters());
                }   
            else
                {
                    for (int i = 0; i < instanceNames.Length; i++)
                    {
                        counters.AddRange(mycat.GetCounters(instanceNames[i]));
                    }
                }

                // Add the retrieved counters to the list.
                foreach (System.Diagnostics.PerformanceCounter counter in counters)
                {
                    this.lstCounter.Items.Add(counter.CounterName);
                }
            }
        }

        private void lstCounter_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstInstance.Items.Clear();
            PerformanceCounterCategory perfCat = new PerformanceCounterCategory(lstCategory.SelectedItem.ToString());
            string[] catInstances; 
            catInstances = perfCat.GetInstanceNames();
            lstInstance.Items.Clear();
            foreach (string catInstance in catInstances)
            {
                lstInstance.Items.Add(catInstance);
            }
            if (lstInstance.Items.Count == 0)
            {
                lstInstance.Enabled = false;
            }
            else
            {
                lstInstance.Enabled = true;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                Counter c = new Counter();
                c.CounterName = lstCounter.SelectedItem.ToString();
                c.Instance = (lstInstance.Enabled ? lstInstance.SelectedItem.ToString() : "");
                c.Object = lstCategory.SelectedItem.ToString();

                SelectedCounter = c;

                this.DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }
    }
}
